#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTabBar>
#include <QInputDialog>
#include <QCompleter>
#include <QFileSystemModel>
#include <QSplitter>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tabWidget->tabBar()->setAutoHide(false);// 即使只有一个标签页也显示标签栏
    ui->splitter->setSizes(QList<int>() << 100 << 600);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::addTab(CodeEditing *codeEditing, const QString &fileName, const QString &suffix)
{
    connect(codeEditing, &CodeEditing::amend, this, [this](const bool b){
        int currentIndex = ui->tabWidget->currentIndex();
        if (currentIndex == -1) return;
        QString s = ui->tabWidget->tabText(currentIndex);

        if (b){
            s.chop(1);
            qDebug() << s;
            ui->tabWidget->setTabText(currentIndex, s);
            return;
        }
        ui->tabWidget->setTabText(currentIndex, s + "*");
    });
    codeEditing->linguisticType(suffix);
    ui->tabWidget->addTab(codeEditing, fileName);
}

void MainWindow::on_newFile_triggered()//新建
{
    QString fileName = QInputDialog::getText(this, tr("新建"), tr("输入文件名："), QLineEdit::Normal);
    if (fileName.isEmpty()) return;
    QString suffix = fileName.section('.', -1); // 获取最后一个'.'后面的部分

    CodeEditing *codeEditing = new CodeEditing(fileName);
    addTab(codeEditing, fileName, suffix);
}

void MainWindow::on_openFile_triggered()//打开
{
    QString fileName = QFileDialog::getOpenFileName(this);
    QFileInfo f(fileName);

    CodeEditing *codeEditing = new CodeEditing(f.fileName());
    codeEditing->loadText(fileName);
    addTab(codeEditing, f.fileName(), f.suffix());
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)//关闭
{
    // 获取要移除的标签页的QWidget
    CodeEditing *codeEditing = qobject_cast<CodeEditing*>(ui->tabWidget->widget(index));
    ui->tabWidget->removeTab(index);
    delete codeEditing;
}

void MainWindow::on_save_triggered()
{
    if (ui->tabWidget->count() == 0) return;

    CodeEditing *codeEditing = qobject_cast<CodeEditing*>(ui->tabWidget->currentWidget());
    if (codeEditing != nullptr){
        qDebug() << "开始保存";
        codeEditing->saveFile();
    }
}

void MainWindow::on_comboBox_activated(int index)
{
    //初步实现切换高亮
    CodeEditing *codeEditing = qobject_cast<CodeEditing*>(ui->tabWidget->widget(ui->tabWidget->currentIndex()));
    codeEditing->ChangeHighlighting(static_cast<Language>(index));
    qDebug() << index;
}

void MainWindow::on_openFolder_triggered()
{
    // QFileSystemModel *fileModel = new QFileSystemModel(this);

    // // 设置根目录（可以是项目的根路径）
    // fileModel->setRootPath(QDir::currentPath());
    // ui->treeView->setModel(fileModel);
    // ui->treeView->setRootIndex(fileModel->index(QDir::currentPath()));

    // // 只显示第一列（名称和图标）
    // for (int i = 1; i < fileModel->columnCount(); ++i) {
    //     ui->treeView->setColumnHidden(i, true);  // 隐藏其他列
    // }

    // 可选：调整列宽度，确保文件名和图标显示得足够好
    // treeView->setHeaderHidden(true);  // 隐藏表头
    // treeView->setItemsExpandable(true);
    // treeView->setIndentation(20);  // 设置缩进
    // treeView->expandAll();  // 可选：自动展开所有节点
}


void MainWindow::on_FileM_triggered()
{

}

